#include "playslider.h"
#include "ui_playslider.h"
#include "util.h"

PlaySlider::PlaySlider(QWidget *parent)
    : QWidget(parent)
    , ui(new Ui::PlaySlider)
{
    ui->setupUi(this);
}

PlaySlider::~PlaySlider()
{
    delete ui;
}

void PlaySlider::setPlayStep(double stepRatio)
{
    playGrogress = stepRatio * ui->inLine->width();
    moveSlider();
}

void PlaySlider::mousePressEvent(QMouseEvent *event)
{
    // 鼠标按下时, 记录当前鼠标的x坐标, 以x坐标为outLine的宽度进行更新
    if(event->button() == Qt::LeftButton) {
        playGrogress = event->pos().x();
        moveSlider();
        return ;
    }
    QWidget::mousePressEvent(event);
}

void PlaySlider::mouseReleaseEvent(QMouseEvent *event)
{
    // 鼠标松开时, 记录当前鼠标的x坐标, 以x坐标为outLine的宽度进行更新
    if(event->button() == Qt::LeftButton) {
        playGrogress = event->pos().x();
        moveSlider();

        // 向播放窗口发送信号
        emit setPlayProgress((double)playGrogress / ui->inLine->width());

        return ;
    }
    QWidget::mouseReleaseEvent(event);
}

void PlaySlider::mouseMoveEvent(QMouseEvent *event)
{
    // 检测鼠标的位置是否在播放进度条窗口中
    if(!this->geometry().contains(event->pos())){
        LOG()<<event->pos();
        return;
    }

    if(event->buttons() == Qt::LeftButton) {
        playGrogress = event->pos().x();
        if(playGrogress < 0) {
            playGrogress = 0;
        }
        if(playGrogress > this->width()) {
            playGrogress = this->width();
        }
        moveSlider();
        return ;
    }
    QWidget::mouseReleaseEvent(event);
}

void PlaySlider::moveSlider()
{
    ui->outLine->setGeometry(ui->outLine->x(), ui->outLine->y(), playGrogress, ui->outLine->height());
}
